<?php

namespace frontend\models;

use Yii;
use common\helpers\StringHelper;

/**
 * 课程检索模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ShareCourse extends \common\models\course\Course {

    public function fields() {
        return [
            'id',
            'name',
            'thumb',
            'begintime' => function () {
                return date('Y-m-d H:i', $this->begin_time);
            },
            'duration' => function () {
                return StringHelper::trimBlank(Yii::$app->formatter->asDuration($this->duration));
            },
            'limit',
            'age_range' => 'ageRange',
            'teacher' => function () {
                if (!$this->teacher) {
                    return false;
                }
                return $this->teacher->toArray([
                    'id',
                    'name',
                    'avatar',
                    'grade',
                    'gender',
                    'description',
                    'followed',
                ]);
            },
            'price',
            'body',
            'finished',
            'enroll_end_time',
            'can_enroll' => function () {
                if ($this->finished) {
                    return false;
                }
                if ($this->enroll_begin_time < time() && $this->enroll_end_time > time()) {
                    return true;
                }
                return false;
            },
            'service_telephone' => function () {
                return $this->school ? $this->school->telephone : false;
            },
            'place',
        ];
    }

}
